package com.springboot.last_experiment.service;

import com.springboot.last_experiment.dao.HealthDao;
import com.springboot.last_experiment.dao.UserDao;
import com.springboot.last_experiment.entity.Health;
import com.springboot.last_experiment.entity.User;
import com.springboot.last_experiment.vo.ResultVO;
import com.springboot.last_experiment.voutils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Transactional
@Service
public class HealthService {
    @Autowired
    HealthDao healthDao;
    @Autowired
    UserDao userDao;

    public ResultVO health_insert(Health health, String skey){
        User user1 = userDao.selectBySkey(skey);
        if(user1==null||user1.getRole()!=1){
            return ResultVOUtil.error(500,"skey校验失败");
        }
        User user = userDao.selectById(health.getOpenid());
        health.setResident(user);
        LocalDateTime time =LocalDateTime.now();
        //time.format(DateTimeFormatter.BASIC_ISO_DATE);
        health.setTime(time.withNano(0));
        Float temperature = health.getTemperature();
        if(temperature>38||health.getIsContact()==1){
            user.setStatus(2);
            user.setIsLock(1);
        }else if(temperature>37||health.getIsArrive()==1){
            user.setStatus(1);
        }else {
        }
        try {
            userDao.update(user);
            healthDao.insertHea(health);
        }catch (Exception e){
            return ResultVOUtil.error(500,"添加异常");
        }

        //更新用户当前身体状况
        if(user!=null){
            user.setStatus(health.getStatus());
            try {
                userDao.update(user);
            }catch (Exception e){
                return ResultVOUtil.error(500,"更新用户当前身体状况异常");
            }
        }
        return ResultVOUtil.success(null);
    }

    public ResultVO getAllHea(){
        List<Health> allHea = healthDao.getAllHea();
        for(Health health:allHea){
            health.setResident(userDao.selectById(health.getOpenid()));
        }
        return ResultVOUtil.success(allHea);
    }

    public ResultVO getHeaById(Integer id){
        Health health = healthDao.getHeaById(id);
        health.setResident(userDao.selectById(health.getOpenid()));
        return ResultVOUtil.success(health);
    }

    public ResultVO getHeaByKey(String key){
        boolean flag = true;
        switch (key){
            case "正常":key="0";flag=true;break;
            case "预警":key="1";flag=true;break;
            case "高危":key="2";flag=true;break;
            case "进":key="0";flag=false;break;
            case "出":key="1";flag=false;break;
            default:break;
        }
        List<Health> heaByKey = healthDao.getHeaByKey(key,flag);
        for(Health health:heaByKey){
            health.setResident(userDao.selectById(health.getOpenid()));
        }
        return  ResultVOUtil.success(heaByKey);
    }

    public ResultVO updateHea(Health health){
        Health health2=healthDao.getHeaById(health.getHealthId());
        User user = userDao.selectById(health2.getOpenid());
        //status由后端判断（37.3以下正常，体温正常但接触或到达过也是预警，37.4-39预警,39以上高危）
        Float temperature = health.getTemperature();
        if(temperature>39){
            health.setStatus(2);
        }else if(temperature>37.3){
            health.setStatus(1);
        }else if(health.getIsArrive()==1||health.getIsContact()==1){
            health.setStatus(1);
        }else {
            health.setStatus(0);
        }
        LocalDateTime time =LocalDateTime.now();
        health.setTime(time.withNano(0));
        try {
            healthDao.updateHea(health);
        }catch (Exception e){
            return ResultVOUtil.error(500,"修改异常");
        }
        //更新用户当前身体状况
        if(user!=null){
            user.setStatus(health.getStatus());
            try {
                userDao.update(user);
            }catch (Exception e){
                return ResultVOUtil.error(500,"更新用户当前身体状况异常");
            }
        }
        return  ResultVOUtil.success(null);
    }

    public ResultVO getWarn(){
        List<User> users = userDao.selectWarn();
        return ResultVOUtil.success(users);
    }

    public ResultVO getDanger(){
        List<User> users = userDao.selectDanger();
        return ResultVOUtil.success(users);
    }

     public ResultVO getUserByOpenid(String openid){
         List<Health> heaByOpenid = healthDao.getHeaByOpenid(openid);
         return ResultVOUtil.success(heaByOpenid);
    }



}
